package com.sgb.contract.view.ui.accountdetail;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivityAccountDetailBinding;
import com.sgb.contract.utils.ArouterConfigs;


/**
 * 描述：在线合同开户信息
 *
 * @author chentao
 */
@Route(path = ArouterConfigs.AR_ACCOUNT_DETAIL)
public class AccountDetailActivity extends BaseActivity<ContractActivityAccountDetailBinding, AccountDetailViewModel> {

    public static final String KEY_DETAIL = "key_detail";
    private boolean isOnResumed = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_account_detail;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(AccountDetailViewModel.class, BR.mAccountDetailViewModel);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AccountDetailActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText(R.string.name_title_account_info);
        mViewModel.getCompanyDetail(AccountDetailActivity.this);
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.contractEditManager.setOnClickListener(v -> {
            ARouter.getInstance().build(ArouterConfigs.AR_ACCOUNT_EDIT).withSerializable(KEY_DETAIL, mViewModel.detail.getValue()).navigation();
        });
    }

    @Override
    @SuppressLint("SetTextI18n")
    protected void initObserve() {
        mViewModel.detail.observe(this, type -> {
            if (type != null) {
                mDataBinding.companyName.setText(type.companyName);
                mDataBinding.companyNo.setText(type.creditCode);
                if (!TextUtils.isEmpty(type.phone) && type.phone.length() == 11) {
                    String pho1 = type.phone.substring(0, 3);
                    String pho2 = type.phone.substring(type.phone.length() - 4);
                    mDataBinding.edPhone.setText(pho1 + "****" + pho2);
                }
                mDataBinding.accountNum.setText(type.csUserId);
                mDataBinding.name.setText(type.name);
                if (!TextUtils.isEmpty(type.identityCard)) {
                    String ID1 = type.identityCard.substring(0, 4);
                    String ID2 = type.identityCard.substring(type.identityCard.length() - 4);
                    mDataBinding.idNum.setText(ID1 + "*********" + ID2);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isOnResumed) {
            isOnResumed = true;
            return;
        }
        mViewModel.getCompanyDetail(AccountDetailActivity.this);
    }
}
